// pages/order/order.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    state: [
      {
        attribute: '待发货'
      },
      {
        attribute: '已发货'
      },
      {
        attribute: '已收货'
      },
      {
        attribute: '退款中'
      },
      {
        attribute: '已退款'
      },
    ],
    stateValue: 0,
    page: 1,
    pagesize: 10,
    list: []

  },
  // bindtap函数,改变导航栏状态
  changeSelected(e) {
    this.setData({
      stateValue: e.target.dataset.index
    })
    this.showList()
  },
  // 显示订单数据
  showList() {
    wx.request({
      url: 'http://124.222.95.170:21311/order/myGoods',
      method: 'GET',
      header: {
        'Cookie': wx.getStorageSync('sessionid')
      },
      data: {
        status: this.data.stateValue
      },
      success: (res) => {
        if(res.data.msg == "NOTLOGIN"){
          wx.showToast({
            title: '登陆后才能使用该功能哦',
            icon:'none'
          })
          setTimeout(function() {
            // 1.3s后返回
            wx.switchTab({
              url: '/pages/users/users',
              success: function(res) {
                console.log('未登录，返回到个人页面')
              },
              fail: function(res) {},
              complete: function(res) {},
            })
          }, 1300);
        }else
        {console.log(res)
        this.setData({
          list: res.data.data
        })
        console.log('list', this.data.list)}
      }
    })
  },
  //去详情页
  showdetail(e) {
    console.log(e)
    const book = JSON.stringify(e.currentTarget.dataset.book)
    wx.navigateTo({
      url: `/pages/orderDetail/orderDetail?book=${book}`,
    })

  },
  //确认发货,填写发货订单
  share(e) {
    var _this = this
    wx.showModal({
      title: '请填写快递单号',
      editable: true,
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          console.log('快递单号', res.content)
          const temp = e.currentTarget.dataset.index
          console.log('temp', temp)
          const bookID = _this.data.list[temp].orders.id
          console.log('id', bookID)
          wx.request({
            url: `http://124.222.95.170:21311/order/delivery`,
            method: 'PUT',
            data: {
              "id": bookID,
              "trackingNum": res.content
            }
          })
          wx.showToast({
            title: '填写成功',
          })
          _this.onLoad()

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //处理退款
  handleRefund(e) {
    wx.showActionSheet({
      itemList: ['同意退款', '拒绝退款'],
      alertText: '是否同意退款申请',
      success: function (res1) {
        const tapIndex = res1.tapIndex;
        if (tapIndex == 0) {
          console.log('用户点击确定')
          const temp = e.currentTarget.dataset.index
          console.log('temp', temp)
          const bookID = _this.data.list[temp].orders.id
          console.log('id', bookID)
          wx.request({
            url: `http://124.222.95.170:21311/order/refundResponse?id=${bookID}&response=1`,
            method: 'PUT',
            success: (res) => {
              console.log('tui', res)
              wx.showToast({
                title: '退款成功',
              })
              _this.onLoad()
            }
          })
        }
        if (tapIndex == 1) {
          console.log('用户点击拒绝')
          wx.request({
            url: `http://124.222.95.170:21311/order/refundResponse?id=${bookID}&response=0`,
            method: 'PUT',
            success: (res) => {
              console.log('tui', res)
              wx.showToast({
                title: '拒绝退款',
              })
              _this.onLoad()

            }
          })
        }
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    });
  },





  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.showList()


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})